﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Threading.Tasks;

namespace PassGenerator
{
    public partial class frmMobile : Form
    {
        public frmMobile()
        {
            InitializeComponent();
            
        }

        private string NumberFile;

        private void btnbutton1_Click(object sender, EventArgs e)
        {
            try
            {
                int groupNumber = int.Parse(txtGroupNumber.Text);
                allNumbertxt = txtNumberFile.Text;
                //shtml">1300390</a></li>
                Regex reg = new Regex(txtReg.Text);
                string fileText = File.ReadAllText(allNumbertxt);
                var mc = reg.Matches(fileText);
                for (int i = 0; i < mc.Count; i++)
                {
                    Numbers.Add(mc[i].Groups[groupNumber].ToString());
                }
                MessageBox.Show("获取" + Numbers.Count.ToString() + "个号码段");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }


        }

        private string allNumbertxt;
        private List<string> Numbers = new List<string>();
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtNumberFile.Text = openFileDialog1.FileName;
            }
        }

        private int passLength;
        private void txtStart_Click(object sender, EventArgs e)
        {
            if (Numbers.Count == 0 || string.IsNullOrEmpty(txtSavePath.Text))
            {
                MessageBox.Show("没有手机号码段,是否未选择生成号码段?");
            }
            else
            {
                passLength = int.Parse(txttextBox1.Text);
                saveFilePath = txtSavePath.Text;
                prog1.Maximum = Numbers.Count;

                Task t = Task.Factory.StartNew(
                 () =>
                 {
                     using (StreamWriter sw = new StreamWriter(saveFilePath, true))
                     {
                         for (int n = 0; n < Numbers.Count; n++)
                         {
                             int l = Numbers[n].Length;
                             int delta = passLength - l;
                             string formatLength = "";
                             for (int j = 0; j < delta; j++)
                             {
                                 formatLength += "0";
                             }
                             string format = "{0}{1:" + formatLength + "}";
                             int max = (int)Math.Pow(10, delta);

                             for (int i = 0; i < max; i++)
                             {
                                 string data = string.Format(format, Numbers[n], i);
                                 sw.WriteLine(data);
                             }
                             sw.Flush();

                             this.Invoke(
                               (Action)(() =>
                                 {
                                     prog1.Value = n;
                                 })
                                 );
                         }
                         MessageBox.Show("生成完毕");
                     }
                 }

                    );
            }
        }

        private string saveFilePath;
        private void button2_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtSavePath.Text = saveFileDialog1.FileName;
            }
        }

        private void txtNumberFile_DragDrop(object sender, DragEventArgs e)
        {

        }

        private void txtNumberFile_DragEnter(object sender, DragEventArgs e)
        {
            var f = e.Data.GetData("FileDrop") as string[];
            txtNumberFile.Text = f[0];

        }
    }
}
